/**
 * Created by LXH on 2017/10/31.
 */
import axios from './axios.js'
import qs from 'qs'
import store from'../vuex/'
import {tl} from'../mixin/tool'
import {utility} from '../assets/js/utility/utility'

export const loginService = {
    api: {},
    /* 获取Geetest */
    getGaptchas: function (params) {
        return axios.get('/gaptchas' + tl.getParam(params))
    },
    /* 校验Geetest */
    postGaptchas: function (params) {
        return axios.post('/gaptchas',qs.stringify(params))
    },
    /* 通用：验证验证码的对错 */
    postValidateImg: function (params) {
        return axios.post('/kaptchas',qs.stringify(params))
    },
    /* 通用：获取短信验证码 */
    getValidateMess: function (params) {
        return axios.get('/sms' + tl.getParam(params))
    },
    getOauth: function (params) {
        return axios.get('/tokens/oauth' + tl.getParam(params))
    },
    getWXLoginUrl: function (params) { /* 使用微信登录，生成微信登录二维码 */
        return axios.get('/api/get/getWXLoginUrl' + tl.getParam(params))
    },
    registerNext:function(params){
        return axios.post('/users/next', qs.stringify(params))//注册下一步
    },
    
    findsus:function(params){ //忘记密码第二部
        return axios.put('/passwords/next' + tl.getParam(params));
    },
    findphone:function(params){ //忘记密码第三部
        return axios.put('/passwords/phone' + tl.getParam(params));
    },
    phoneLogin: function (params) {
        // return axios.post('/tokens/phone', qs.stringify(params))//需要先注册
        return axios.post('/tokens/bindphone', qs.stringify(params))
    },
    bindPhoneLogin: function (params) {
        return axios.post('/tokens/bindphone', qs.stringify(params))
    },
    noLogin: function() { // 退出登录
        return axios.delete('/tokens');
    },
    pwdLogin: function (params) {
        return axios.post('/tokens/password', qs.stringify(params))
    },
    getWXbind: function(params) {
        return axios.post('/tokens/wxbind', qs.stringify(params));
    },
    getWXbindNext: function(params) {
        return axios.post('/tokens/wxbind/next', qs.stringify(params));
    },
    getWXByCode: function(params) {
        return axios.get('/tokens/wxinfo' + tl.getParam(params));
    },
    getWXbaseCode: function(params) {
        return axios.get('/tokens/wxbaseinfo' + tl.getParam(params));
    },
    getWBByCode: function(params) {
        return axios.get('/tokens/weiboinfo' + tl.getParam(params));
    },
    getQQByCode: function(params) {
        return axios.get('/tokens/qqinfo' + tl.getParam(params));
    },
    bindPhone: function (params) { //绑定手机号
        return axios.put('/users' + tl.getParam(params));
    },
    bindRegister:function (params){ //完善第三方登录注册信息
        return axios.post('/users/third' + tl.getParam(params));
    },
    register: function (params) { 
        return axios.post('/users' , qs.stringify(params));
    },
    friendRegister:function(params){
        return axios.post('/users/invite', qs.stringify(params))//邀请好友注册
    },
    findPwd: function (params) {
        return axios.put('/passwords/phone' + tl.getParam(params));
    },
    modifyPwd: function (params) {
        return axios.put('/passwords/password' + tl.getParam(params));
    },
    packageUserInfo (info) {
        let that = this
        let Base64 = require('js-base64').Base64;
        let obj = eval('('+ Base64.decode(info) +')');
        store.state.userInfo.username = obj.name
        store.state.userInfo.userId = obj.userId
        store.state.userInfo.platform = obj.platform
        store.state.userInfo.exp = obj.exp
        store.state.userInfo.nbf = obj.nbf
        console.log(store.state.userInfo.userId);
    },
    /* 通用：获取验证码 */
    getValidateImg: function (params) {
        return axios.get('/kaptchas' + tl.getParam(params))
    },
    /* 显示密码和隐藏密码切换 */
    hideExchange: function(obj) {
        let part = obj.parentNode;
        let first = part.firstChild;
        if (obj.className === 'iconfont icon-biyan'){
            first.setAttribute('type','password');
            obj.className = 'iconfont icon-yanjing';
        }
        else if (obj.className === 'iconfont icon-yanjing'){
            first.setAttribute('type','text');
            obj.className = 'iconfont icon-biyan';
        }
    },
    /* 图片验证码等一系列 */
    showFitIcon (obj,isRight) {
        let parentN = obj.parentNode;
        let icon =parentN.getElementsByTagName('i');
        if(isRight){
            icon[0].style.display = 'none';
            icon[1].style.display = 'block';

        }else {
            icon[0].style.display = 'block';
            icon[1].style.display = 'none';
        }
    },
    validateImage: function (obj, event) {
        let parentN = obj.parentNode;
        let icon =parentN.getElementsByTagName('i');
        //console.log('测试', obj.value)
        if(event === 'keyup') {
            if(obj.value.trim().length <= 0 ){
                for(let i=0;i< icon.length;i++){
                    if(icon[i].className === 'iconfont icon-shuaxin1'){
                        icon[i].style.display = 'block'
                    }else{
                        icon[i].style.display = 'none'
                    }
                }
            }
        }else if(event === 'click') {
            if(obj.value.trim().length <= 0 ){
                for(let i=0;i< icon.length;i++){
                    if(icon[i].className === 'iconfont icon-shuaxin1'){
                        icon[i].style.display = 'block'
                    }else{
                        icon[i].style.display = 'none'
                    }
                }
            }else {
                for(let i=0;i< icon.length;i++){
                    if(icon[i].className === 'iconfont icon-shuaxin1'){
                        icon[i].style.display = 'none'
                    }else{
                        icon[i].style.display = 'block'
                    }
                }
            }
        }
    },
    /* 清空帐号文本框里面的内容 */
    iptLength (obj) {
        let parent = obj.parentNode;
        let lastChild = parent.lastChild;
        if(obj.value.trim().length > 0 ){
            lastChild.className = 'iconfont icon-Shape'
        }else{
            lastChild.className = 'iconfont icon-Shape hide'
        }
    },
    /* 获取短信验证码的效果功能 */
    validateFromPhone: function (obj, id, flag) {
        let timer;
        if(flag == 0) {
            var count = 0;
            timer = setInterval(function () {
                count = parseInt(document.getElementById('countDown').innerHTML) - 1;
                if(count > 0) {
                    document.getElementById('countDown').innerHTML = count;
                }
                else{
                    clearInterval(timer);
                    obj.innerHTML = '获取验证码';
                    obj.className = 'validateFromPhone'
                    flag = 1;
                }
            },1000);
        }
        return {
            isFlag: flag
        }
        /*if(obj.className === 'validateFromPhone'){
         obj.className = 'validateFromPhone validateWaiting'
         obj.innerHTML = '(<span id="countDown">60</span>s)重新获取';
         //var timer,isLoginOn = flag;
         var count = 0;
         timer = setInterval(function () {
         count = parseInt(document.getElementById('countDown').innerHTML) - 1;
         if(count > 0) {
         document.getElementById('countDown').innerHTML = count;
         }
         else{
         clearInterval(timer);
         obj.innerHTML = '获取验证码';
         obj.className = 'validateFromPhone'
         }
         },1000);
         }*/
    },
    /* 表单验证 */
    formValidate (isPhone, isPic, isSms, isPwd, params) {
        //1.验证手机号是否为空
        //2. 验证手机号是否正确
        //3.验证图片验证码是否为空
        //4.验证短信验证码是否为空
        //5.密码是否为空
        //6.密码是否是6-24位的字母数字组合

        if(isPhone){ //是否验证手机号
            if(!tl.isEmpty(params.phoneNo)){
                return '请输入手机号';
            }
            if(!tl.isPhoneNumber(params.phoneNo)){
                return '请输入正确的手机号';
            }
        }
        if(isPic){ //是否验证图片验证码
            if(!tl.isEmpty(params.kaptchaValue)){
                return "请获取验证码";
            }
        }
        if(isSms){ //是否验证短信验证码
            if(!tl.isEmpty(params.smsValue)){
                return "请输入短信验证码";
            }
        }
        if(isPwd){ //是否验证密码
            if(!tl.isEmpty(params.pwd)){
                return '请输入密码';
            }
            if(!tl.isBetweenMAndN(params.pwd, 6, 24)){
                return '请输入6~24位数字、字母密码组合';
            }
            if(tl.isEnLetter(params.pwd)){//是否只有英文字母
                return '请输入6~24位数字、字母密码组合';
            }
            if(tl.isNumber(params.pwd)){ // 是否只有数字
                return '请输入6~24位数字、字母密码组合';
            }
        }
    },
    getPlatForm () {
        let isWx = utility.is_weixin().isWX;
        if(isWx){
            return 'WXH5';
        }else{
            return 'WAPH5';
        }
    }
}
// export default {loginService}
